<?php





namespace app\shop\controller;



use app\admin\controller\ApiCommon;

use app\shop\model\Order as OrderModel;

use app\shop\model\Warehouse as WarehouseModel;

use app\shop\model\Goods as GoodsModel;

use app\shop\model\PayModel as PayModel;

use app\crm\model\Customer as CustomerModel;

use app\shop\model\RefundPayModel;

use Kingdee\Kingdee;

use think\facade\Db;

use think\response\Json;


/**

 * 支付类

 * @package app\shop\controller

 * @author SL

 * @create 2020-06-01

 */

class Pay extends ApiCommon
{
    /**

     * 用于判断权限

     * @permission 无限制

     * @allow 登录用户可访问

     * @author SL

     * @create 2021-06-01

     */

    public function initialize()

    {



        $action = [

            'permission' => ['get_pay_res','get_paykey','get_returl','addterm'],

            'allow' => ['export']

        ];

        event('CheckAuth', $action);

        $request = $this->request;

        $a       = strtolower($request->action());

        if (!in_array($a, $action['permission'])) {

            parent::initialize();

        }

    }

    //通联支付变更商户号前置操作
    public function addterm()
    {
        try {
            $shop_where = [];
            array_push($shop_where,['shop_id','=',73782775132896]); //需要改的地方
            $shop_info = Db::name('shop')->where($shop_where)->find();
            $shop_info['termaddress'] = '云南昆明官渡区日新路银海森林东门1663号欣奕除疤';   //需要改的地方-
            $shop_info['cusid'] = $shop_info['shop_sihu_cusid'];  //需要改的地方 -根据类型判断，是私户还是公户
            $shop_info['appid'] = $shop_info['shop_sihu_appid'];  //需要改的地方 -根据类型判断，是私户还是公户
            $shop_info['private_key'] = $shop_info['shop_sihu_privateKey'];   //需要改的地方 根据类型判断，是私户还是公户
            //$shop_info['termno'] = $shop_info['termno'];

            $RefundPay = new RefundPayModel();
            $res = $RefundPay->addterm($shop_info);
            var_dump($res);
        }catch (\Exception $e) {
            var_dump($e->getMessage());
        }
    }



    /**

     * 扫码盒子支付提交

     * @return Json

     * @author SL

     * @create 2021-06-01

     */

    public function payBox(): Json

    {

		$this->param['userId']=$this->userInfo['id'];
		$adminList = Db::name('admin_user')->where('id','=',$this->userInfo['id'])->column('realname,id,shop_id');
        //登录员工的 部门归属
		$this->param['shopIds']=$adminList[0]['shop_id'];

		$payModel = new PayModel();

        $data       = $payModel->payBox($this->param);



        if (!$data) {
            // echo "???";
            return resultArray(['error' => $payModel->getError()]);

        }
        // exit;
        return resultArray(['data'=> [], 'error' => $payModel->getError()]);

        // return resultArray(['data' => $data]);

    }

    /**

     * 请求支付url

     * @return Json

     * @author SL

     * @create 2021-06-01

     */

    public function getPayUrl(): Json

    {

		$this->param['userId']=$this->userInfo['id'];
		$adminList = Db::name('admin_user')->where('id','=',$this->userInfo['id'])->column('realname,id,shop_id');

		//登录员工的 部门归属
		$this->param['shopIds']=$adminList[0]['shop_id'];
        $payModel = new PayModel();

        $data       = $payModel->getPayUrl($this->param);


        // echo "<pre>";
        // print_r($data);
        // echo "</pre>";
        // exit;

        // $data = [
        //     'url' => 'https://qr.alipay.com/bax03279pv4gexppq7gv0099'
        // ];
        return resultArray(['data' => $data]);

    }

    /**

     * 支付结果

     * @return Json

     * @author SL

     * @create 2021-06-01

     */

    public function get_pay_res()

    {

        // echo "支付结果";
        // echo "<br/>";
        // exit;

        $response = file_get_contents('php://input');

        // $file = "text2.txt";

        // file_put_contents($file, $response);
        $payModel = new PayModel();

        $data       = $payModel->getPayBack($response);

        return $data;

    }

    public function get_paykey()
    {
        // echo "qingqiu";
        // echo "<br/>";
        // exit;

        $request = request()->param();

        $order_id = $request['refund_order_no'] ?? 0;

        $shop_id = $request['shop_id'] ?? 0;

        $where = [];

        array_push($where,['refund_order_no','=',$order_id]);

        $oredr_info = Db::name('shop_refund_order')->where($where)->find();

        $shop_where = [];

        array_push($shop_where,['shop_id','=',$shop_id]);

        $shop_info = Db::name('shop')->where($shop_where)->find();

        if($oredr_info['shanghu_type'] == '1'){
            // 公户

            $cusid = $shop_info['shop_gonghu_cusid'];

            $appid = $shop_info['shop_gonghu_appid'];

            $private_key = $shop_info['shop_gonghu_privateKey'];

        }else{
            // 私户

            $cusid = $shop_info['shop_sihu_cusid'];

            $appid = $shop_info['shop_sihu_appid'];

            $private_key = $shop_info['shop_sihu_privateKey'];

        }

        if($shop_info['shop_type'] == 1 ){
            //门诊
            $pay_amount = bcadd($oredr_info['pay_amount'],$oredr_info['shop_transfer_amount'],2);
        }else{
            //门店
            $pay_amount = $oredr_info['pay_amount'];
        }

        $payRequest = [

            'pay_amount' =>$pay_amount,

            'refund_order_no' => $oredr_info['refund_order_no'],

            'description' => $oredr_info['description'],

            'cusid' => $cusid,

            'appid' => $appid,

            'private_key' => $private_key,

            'shop_name' => '欣奕除疤'.$shop_info['city_name'].$shop_info['shop_name'],

            'shop_id' => $shop_id,

            'shanghu_type' => $oredr_info['shanghu_type'],

            'is_pay' => $oredr_info['is_pay'],

        ];

        return resultArray(['data' => $payRequest]);
    }


}
